package com.example.scale;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.color.Color;

import com.example.scale.SceneManager.SceneType;

public class SkillsScene extends BaseScene implements IOnMenuItemClickListener {
	
	private MenuScene menuChildScene;
	
	private MenuScene mPopupScene;
	
	private final int MENU_ACCEPT = 0;
	private final int MENU_CANCEL = 1;
	
	private boolean popupIsShowing;
	
	@Override
	public void createScene() {
		createPopupScene();
		attachChild(new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2,
				resourcesManager.blank_background_region, vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
		
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);
		
		Sprite optionsHeader = new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT-60,
				resourcesManager.skills_region, vbom);
		optionsHeader.setScale(.6f);
		attachChild(optionsHeader);
		
		final Sprite backButton = new Sprite(45, GameActivity.CAMERA_HEIGHT-45, resourcesManager.arrow_region, vbom) {
			public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y) {
				if (touchEvent.isActionDown()) {
					onBackKeyPressed();
				}
				return true;
			}
		};
		backButton.setScale(0.5f);
		attachChild(backButton);
		registerTouchArea(backButton);
		
		final Sprite testButton = new Sprite (GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2, resourcesManager.arrow_region, vbom) {
			public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y) {
				if (touchEvent.isActionDown()) {
					showPopupScene();
				}
				return true;
			}
		};
		testButton.setScale(0.5f);
		attachChild(testButton);
		registerTouchArea(testButton);
	}
	
	public void showPopupScene() {
		setChildScene(mPopupScene, false, true, true);
		popupIsShowing = true;
	}
	
	public void createPopupScene() {
		popupIsShowing = false;
		mPopupScene = new MenuScene(camera);
		Rectangle textRectangle = new Rectangle(GameActivity.CAMERA_WIDTH / 2,
				GameActivity.CAMERA_HEIGHT / 2, GameActivity.CAMERA_WIDTH - 150,
				GameActivity.CAMERA_HEIGHT / 2+200, vbom);
		textRectangle.setColor(Color.BLACK);
		//textRectangle.setAlpha(.8f);
		mPopupScene.attachChild(textRectangle);
		
		//setIgnoreUpdate(true);
		
		final IMenuItem acceptButton = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_ACCEPT, resourcesManager.accept_region,
						vbom), 0.6f, 0.5f);
		mPopupScene.addMenuItem(acceptButton);
		final IMenuItem cancelButton = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_CANCEL, resourcesManager.cancel_region,
						vbom), 0.6f, 0.5f);
		mPopupScene.addMenuItem(cancelButton);
		
		mPopupScene.buildAnimations();
		mPopupScene.setBackgroundEnabled(false);
		
		acceptButton.setPosition(GameActivity.CAMERA_WIDTH/2-125, 115);
		cancelButton.setPosition(GameActivity.CAMERA_WIDTH/2+125, 115);
		
		mPopupScene.setOnMenuItemClickListener(this);
	}

	@Override
	public void onBackKeyPressed() {
		SceneManager.getInstance().skillsScene.disposeScene();
		//ResourcesManager.getInstance().unloadSkillsTextures();
		SceneManager.getInstance().loadMenuScene(engine);
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_ACCEPT:
			if (popupIsShowing) {
				popupIsShowing = false;
				clearChildScene();
			}
			return true;
		case MENU_CANCEL:
			if (popupIsShowing) {
				popupIsShowing = false;
				clearChildScene();
			}
			return true;
		default:
			return false;
		}
	}
}
